package com.yiquan.chat.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.yiquan.chat.pojo.Message;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 数据访问接口
 *
 * @author Administrator
 */
public interface MessageDao extends JpaRepository<Message, Long>, JpaSpecificationExecutor<Message> {


    @Query(nativeQuery = true, value = "select * from  yiquan_msg where converse_id = ?1 and msg_state !='2' order by create_date desc limit 1 ")
    Message getLastMsgByConverseId(Long converseId);

    @Query(nativeQuery = true, value = "select * from  yiquan_msg where converse_id = ?1 and msg_state != '2'")
    List<Message> getUnDeleteMessageByConverseId(Long converseId);

    @Query(nativeQuery = true, value = "select count(*) from  yiquan_msg where converse_id = ?1 and msg_state = 0")
    Integer countUnReadMsgByConverseId(Long converseId);

    @Query(nativeQuery = true, value = "select * from  yiquan_msg where converse_id = ?1 and msg_state = 0")
    List<Message> getUnReadMsgByConverseId(Long converseId);

    @Query(nativeQuery = true,value = "update yiquan_msg set msg_state = '1' , msg_receive_date = ?2 where  converse_id = ?1 and msg_state='0'")
    @Modifying
    @Transactional
    void updateMsgUnreadStatus(Long converseId, Date date);
}
